package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.UserBean;
import com.project.bean.UserRoleBean;
import com.project.dao.IUserDao;
import com.project.dao.IUserRoleDao;
import com.project.service.ILogService;
import com.project.service.IUserService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao iUserDao;
    @Autowired
    private IUserRoleDao iUserRoleDao;
    @Autowired
    private ILogService iLogService;

    @Override
    public PageInfo<UserBean> showUserByPage(int pageNum, String username) {
        PageHelper.startPage(pageNum, 3);
        List<UserBean> list;
        if ("1".equals(username)) {
            list = iUserDao.selectList(null);
        } else {
            list = iUserDao.selectList(new QueryWrapper<UserBean>().like("username", username));
        }
        for (int i = 0; i < list.size(); i++) {
            long userId = list.get(i).getUserId();
            System.out.println("userId=" + userId);
            list.get(i).setRoleBeanList(iUserDao.findRole(userId));
        }
        return PageInfo.of(list);
    }

    @Override
    public InfoResult addUser(String username, int[] roles, String realName) {
        InfoResult re = new InfoResult();
        UserBean userBean = new UserBean();
        userBean.setUsername(username);
        //密码默认为1122
        userBean.setPassword("1122");
        userBean.setRealName(realName);
        int x = iUserDao.insert(userBean);
        long userId = iUserDao.selectOne(new QueryWrapper<UserBean>().eq("username", username)).getUserId();
        for (int i = 0; i < roles.length; i++) {
            UserRoleBean userRoleBean = new UserRoleBean();
            userRoleBean.setUserId(userId);
            userRoleBean.setRoleId(roles[i]);
            iUserRoleDao.insert(userRoleBean);
        }
        if (x == 0) {
            re.setCode(1);
            re.setInfo("添加失败");
        } else {
            re.setCode(0);
            re.setInfo("添加成功");
        }
        return re;
    }

    @Override
    public UserBean findUserById(int userId) {
        return iUserDao.findByUserId(userId);
    }

    @Override
    public InfoResult delUserById(int userId) {
        InfoResult re = new InfoResult();
        iUserDao.deleteById(userId);
        int x = iUserRoleDao.delete(new QueryWrapper<UserRoleBean>().eq("fk_userId", userId));
        if (x > 0) {
            re.setCode(0);
            re.setInfo("删除成功");
        }
        return re;
    }

    @Override
    public InfoResult updateUserById(int userId, int[] roles) {
        int x = 0;
        QueryWrapper<UserRoleBean> wa = new QueryWrapper<>();
        //删除用户当前的权限
        wa.eq("fk_userId", userId);
        int y = iUserRoleDao.delete(wa);
        InfoResult infoResult = new InfoResult();
        for (int i = 0; i < roles.length; i++) {
            UserRoleBean us = new UserRoleBean();
            us.setUserId(userId);
            us.setRoleId(roles[i]);
            iUserRoleDao.insert(us);
            x = i;
        }
        infoResult.setCode(0);
        infoResult.setInfo("当前用户新增加" + (x + 1) + "个角色,删除" + y + "个旧角色");
        return infoResult;
    }

    @Override
    public UserBean userLogin(String username, String password) {
        return iUserDao.userLogin(username, password);
    }

    @Override
    public InfoResult userUpdatePwd(int userId, String oldPassword, String newPassword) {
        UserBean user = iUserDao.selectById(userId);
        //如果不符合旧密码则返回修改失败的信息
        if (!user.getPassword().equals(oldPassword)) {
            return new InfoResult(1, "修改密码失败,原密码不正确");
        }
        user.setPassword(newPassword);
        iUserDao.updateById(user);
        return new InfoResult(0, "修改密码成功");
    }
}
